import scrapy
from sSofa.items import SsofaItem

class ExampleSpider(scrapy.Spider):
    name = 'example'
    myheaders = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }

    def start_requests(self):
        purl = "https://muuto.com/furniture"
        yield scrapy.Request(purl,headers = self.myheaders)

    def parse(self, response):
        div_xapth_list = response.xpath("//a[@class='product photo product-item-photo']/@href").extract()
        for each in div_xapth_list:
            yield scrapy.Request(each,headers = self.myheaders,callback = self.sec_page)

    def sec_page(self,response):
        item = SsofaItem()

        title = response.xpath("//span[@data-ui-id='page-title-wrapper']/text()").extract()
        item['title'] = title 
        
        source_video = response.xpath("//section[@id='video-unbranded']//iframe[@id='player']/@src").extract()
        item['videourl'] = source_video

        xpathfilter ="//section[@id='fifty-fifty-image-slider-text']//img/@src | //section[@id='brand-images']//img/@src | //section[@id='designer']//img/@src"
        srcurl = response.xpath(xpathfilter).extract()
        item['srcurl'] = srcurl

        # srcurl = response.xpath("//section[@id='fifty-fifty-image-slider-text']//img/@src").extract()
        # item['srcurl'] = srcurl

        # brand_images = response.xpath("//section[@id='brand-images']//img/@src").extract()
        # item['srcurl'].append(brand_images)

        # designer = response.xpath("//section[@id='designer']//img/@src").extract()
        # item['srcurl'].append(designer)


        yield item
